package edison.jcr.nodetype;

import javax.jcr.Value;
import javax.jcr.nodetype.NodeDefinition;
import javax.jcr.nodetype.NodeType;
import javax.jcr.nodetype.NodeTypeIterator;
import javax.jcr.nodetype.PropertyDefinition;
import edison.jcr.SessionContext;
import edison.persistence.NodeTypeBean;

public final class NodeTypeImpl extends NodeTypeDefinitionImpl implements NodeType {

	public NodeTypeImpl(SessionContext context, NodeTypeBean bean) {
		super(context, bean);
	}

	public NodeType[] getSupertypes() {
		return nodetypeService.getSupertypeNames(name);
	}

	public NodeType[] getDeclaredSupertypes() {
		return nodetypeService.getDeclaredSupertypes(name);
	}

	public NodeTypeIterator getSubtypes() {
		return nodetypeService.getSubtypeNames(name);
	}

	public NodeTypeIterator getDeclaredSubtypes() {
		return nodetypeService.getDeclaredSubtypeNames(name);
	}

	public boolean isNodeType(String nodeTypeName) {
		return nodetypeService.isSuperTypeOf(name, nodeTypeName);
	}

	public PropertyDefinition[] getPropertyDefinitions() {
		return nodetypeService.getPropertyDefinitions(name);
	}

	public NodeDefinition[] getChildNodeDefinitions() {
		return nodetypeService.getChildNodeDefinitions(name);
	}

	public boolean canSetProperty(String propertyName, Value value) {
		PropertyDefinition propertyDefinition = nodetypeService.getPropertyDefinition(name, propertyName);
		String[] valueConstraints = propertyDefinition.getValueConstraints();
		return check(valueConstraints, value);
	}

	private boolean check(String[] valueConstraints, Value value) {
		return true;
	}

	public boolean canSetProperty(String propertyName, Value[] values) {
		PropertyDefinition propertyDefinition = nodetypeService.getPropertyDefinition(name, propertyName);
		String[] valueConstraints = propertyDefinition.getValueConstraints();
		for (int i = 0; i < values.length; i++) {
			if (check(valueConstraints, values[i])) {
				return false;
			}
		}

		return true;
	}

	public boolean canAddChildNode(String childNodeName) {
		// TODO Auto-generated method stub
		return false;
	}

	public boolean canAddChildNode(String childNodeName, String nodeTypeName) {
		// TODO Auto-generated method stub
		return false;
	}

	public boolean canRemoveItem(String itemName) {
		// TODO Auto-generated method stub
		return false;
	}

	public boolean canRemoveNode(String nodeName) {
		// TODO Auto-generated method stub
		return false;
	}

	public boolean canRemoveProperty(String propertyName) {
		// TODO Auto-generated method stub
		return false;
	}

}